package com.malou.client.reportes;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class EnviarCorreoDialog extends Dialog {
	private TextField<String> destino;
	private TextArea mensaje;
	private Button guardar;
	private Button cancelar;
	public Status status;
	EnviarCorreo send;
	ReporteVentasPanel rptVentas;
	GReporteVentasPanel rptGVentas;
	PrendasVendidasPanel rptPrendasVendidas;
	GPrendasVendidasPanel rptGPrendasVendidas;
	ReporteStockPanel rptStock;
	GReporteStockPanel rptGStock;
	ReporteInventarioPanel rptInventario;
	GReporteInventarioPanel rptGInventario;
	ReporteIngresosTallerPanel rptIngresosTaller;
	GReporteIngresosTallerPanel rptGIngresosTaller;
	ReporteSaldosPanel rptSaldos;
	GReporteSaldosPanel rptGSaldos;
	ReporteSalidasPanel rptSalidas;
	ReporteEntradasPanel rptEntradas;
	ReportePagosTarjetaPanel rptPagosTarjeta;
	
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public EnviarCorreoDialog(){
		//setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		setSize(350, 170);
		setClosable(true);
		
		FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	        
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(destino);
	    
	    setIconStyle("icon-mail");
		setHeading(constants.enviarCorreo());
	}
	
	@Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.enviando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  EnviarCorreoDialog.this.hide();
	      }

	    });

	    guardar = new Button(constants.enviar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
	    		getButtonBar().disable();		    
	    		send = new EnviarCorreo();
	    		send.setDestino(destino.getValue());
	    		send.setMensaje(mensaje.getValue());
	    		if(rptVentas != null) rptVentas.enviarEmail(send);
	    		if(rptGVentas != null) rptGVentas.enviarEmail(send);
	    		if(rptPrendasVendidas != null) rptPrendasVendidas.enviarEmail(send);
	    		if(rptGPrendasVendidas != null) rptGPrendasVendidas.enviarEmail(send);
	    		if(rptStock != null) rptStock.enviarEmail(send);
	    		if(rptGStock != null) rptGStock.enviarEmail(send);
	    		if(rptInventario != null) rptInventario.enviarEmail(send);
	    		if(rptGInventario != null) rptGInventario.enviarEmail(send);
	    		if(rptIngresosTaller != null) rptIngresosTaller.enviarEmail(send);
	    		if(rptGIngresosTaller != null) rptGIngresosTaller.enviarEmail(send);
	    		if(rptSaldos != null) rptSaldos.enviarEmail(send);
	    		if(rptGSaldos != null) rptGSaldos.enviarEmail(send);
	    		if(rptEntradas != null) rptEntradas.enviarEmail(send);
	    		if(rptSalidas != null) rptSalidas.enviarEmail(send);
	    		if(rptPagosTarjeta != null) rptPagosTarjeta.enviarEmail(send);
	    	    		
	    	  }
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	}
	
	private boolean validate() {
		boolean des = destino.isValid();
		boolean men = mensaje.isValid();		
	    return des && men;
	}
	
	public void reset(){
		rptVentas= null;
		rptGVentas= null;
		rptPrendasVendidas=null;
		rptGPrendasVendidas=null;
		rptStock=null;
		rptGStock=null;
		rptInventario=null;
		rptGInventario=null;
		rptIngresosTaller=null;
		rptGIngresosTaller=null;
		rptSaldos=null;
		rptGSaldos=null;
		rptEntradas = null;
		rptSalidas = null;
		rptPagosTarjeta = null;
		destino.clear();		
		mensaje.clear();		
		status.hide();
		getButtonBar().enable();
	}
	
	private void addFields(ContentPanel cp) {
		FormLayout layout =new FormLayout();
		layout.setLabelWidth(60);
		cp.setLayout(layout);
		
		destino = new TextField<String>();		
		destino.setFieldLabel(constants.email());		
		destino.setAllowBlank(false);
		
		final String emailReg = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
	    final String errMsg = constants.emailNoValido();
	    destino.setValidator(new Validator() {
	      public String validate(Field<?> field, String value) {
	        if (!value.toUpperCase().matches(emailReg))
	          return errMsg;
	        return null;
	      }
	    });
		
	    cp.add(destino, new FormData("-20"));
	    
	    mensaje = new TextArea();  
	    mensaje.setPreventScrollbars(true);  
	    mensaje.setFieldLabel(constants.mensaje());	    
	    cp.add(mensaje, new FormData("-20"));
	}
}
